import { ref, onMounted } from 'vue';
import { 
  getHospitalList,
  getHospitalDetail,
  getDepartmentList,
  getDepartmentDetail,
  getDoctorList,
  getDoctorDetail
} from '../services/hospital.service';

/**
 * 医院列表相关逻辑
 */
export function useHospitalList() {
  const loading = ref(false);
  const refreshing = ref(false);
  const finished = ref(false);
  const hospitals = ref<Hospital[]>([]);
  const currentPage = ref(1);
  const pageSize = ref(10);
  const total = ref(0);
  
  // 加载医院列表
  const loadHospitals = async (keyword: string = '') => {
    if (refreshing.value) {
      hospitals.value = [];
      currentPage.value = 1;
      finished.value = false;
    }
    
    try {
      loading.value = true;
      const params = {
        page: currentPage.value,
        pageSize: pageSize.value,
        keyword
      };
      
      const res = await getHospitalList(params);
      
      if (refreshing.value) {
        hospitals.value = [];
        refreshing.value = false;
      }
      
      if (res.data) {
        hospitals.value = [...hospitals.value, ...res.data.list];
        total.value = res.data.total;
        
        // 判断是否已加载完全部数据
        if (hospitals.value.length >= total.value) {
          finished.value = true;
        }
        
        currentPage.value++;
      }
    } catch (error) {
      console.error('加载医院列表失败', error);
    } finally {
      loading.value = false;
    }
  };
  
  // 刷新列表
  const onRefresh = () => {
    refreshing.value = true;
    finished.value = false;
    currentPage.value = 1;
    loadHospitals();
  };
  
  return {
    loading,
    refreshing,
    finished,
    hospitals,
    loadHospitals,
    onRefresh
  };
}

/**
 * 医院详情相关逻辑
 */
export function useHospitalDetail(hospitalId: number) {
  const loading = ref(false);
  const hospital = ref<Hospital | null>(null);
  
  // 加载医院详情
  const loadHospitalDetail = async () => {
    try {
      loading.value = true;
      const res = await getHospitalDetail(hospitalId);
      if (res.data) {
        hospital.value = res.data;
      }
    } catch (error) {
      console.error('加载医院详情失败', error);
    } finally {
      loading.value = false;
    }
  };
  
  onMounted(() => {
    loadHospitalDetail();
  });
  
  return {
    loading,
    hospital,
    loadHospitalDetail
  };
}

/**
 * 科室列表相关逻辑
 */
export function useDepartmentList(hospitalId: number) {
  const loading = ref(false);
  const departments = ref<Department[]>([]);
  
  // 加载科室列表
  const loadDepartments = async () => {
    try {
      loading.value = true;
      const res = await getDepartmentList(hospitalId);
      if (res.data) {
        departments.value = res.data;
      }
    } catch (error) {
      console.error('加载科室列表失败', error);
    } finally {
      loading.value = false;
    }
  };
  
  onMounted(() => {
    loadDepartments();
  });
  
  return {
    loading,
    departments,
    loadDepartments
  };
}

/**
 * 医生列表相关逻辑
 */
export function useDoctorList(params?: { hospitalId?: number; departmentId?: number }) {
  const loading = ref(false);
  const refreshing = ref(false);
  const finished = ref(false);
  const doctors = ref<Doctor[]>([]);
  const currentPage = ref(1);
  const pageSize = ref(10);
  const total = ref(0);
  
  // 加载医生列表
  const loadDoctors = async () => {
    if (refreshing.value) {
      doctors.value = [];
      currentPage.value = 1;
      finished.value = false;
    }
    
    try {
      loading.value = true;
      const queryParams = {
        page: currentPage.value,
        pageSize: pageSize.value,
        ...params
      };
      
      const res = await getDoctorList(queryParams);
      
      if (refreshing.value) {
        doctors.value = [];
        refreshing.value = false;
      }
      
      if (res.data) {
        doctors.value = [...doctors.value, ...res.data.list];
        total.value = res.data.total;
        
        // 判断是否已加载完全部数据
        if (doctors.value.length >= total.value) {
          finished.value = true;
        }
        
        currentPage.value++;
      }
    } catch (error) {
      console.error('加载医生列表失败', error);
    } finally {
      loading.value = false;
    }
  };
  
  // 刷新列表
  const onRefresh = () => {
    refreshing.value = true;
    finished.value = false;
    currentPage.value = 1;
    loadDoctors();
  };
  
  return {
    loading,
    refreshing,
    finished,
    doctors,
    loadDoctors,
    onRefresh
  };
} 